package com.test.pudamoney.activity_fragment_pudamoney.fragment_pudamoney;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.test.pudamoney.R;
import com.test.pudamoney.activity_fragment_pudamoney.UsagePermissionGuideActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseApplication_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseLoanFragment_pudamoney;
import com.test.pudamoney.bean_pudamoney.config_info_pudamoney.AppShowInfo_pudamoney;
import com.test.pudamoney.bean_pudamoney.user_info_pudamoney.LoanLimitInfo_pudamoney;
import com.test.pudamoney.constant_pudamoney.Constant_pudamoney;
import com.test.pudamoney.dialog_pudamoney.CustomerServiceDialog_pudamoney;
import com.test.pudamoney.util_pudamoney.PermissionUtil_pudamoney;
import com.test.pudamoney.util_pudamoney.StringUtil_pudamoney;
import com.test.pudamoney.util_pudamoney.helper_pudamoney.UserInfoHelper_pudamoney;
import com.test.pudamoney.util_pudamoney.http_pudamoney.HttpManager_pudamoney;
import com.test.pudamoney.util_pudamoney.analytics_pudamoney.util_pudamoney.AnalyticsUtil_pudamoney;

import pub.devrel.easypermissions.EasyPermissions;

public class LoanMainFragment_pudamoney extends BaseLoanFragment_pudamoney {

    private TextView loanAmountTv;

    public boolean isRefresh = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_loan_main_pudamoney, container, false);
        init_pudamoney(view);
        return view;
    }

    @SuppressLint("SetTextI18n")
    private void init_pudamoney(View view) {
        loanAmountTv = view.findViewById(R.id.fragment_loan_main_tv_loan_amount);

        TextView limit1Tv = view.findViewById(R.id.fragment_loan_main_tv_limit_1);
        TextView limit2Tv = view.findViewById(R.id.fragment_loan_main_tv_limit_2);
        String moneyUnit = Constant_pudamoney.Money_Unit_Nigeria;
        limit1Tv.setText(moneyUnit + " 800,000");
        limit2Tv.setText(moneyUnit + " 1,000,000");

        view.findViewById(R.id.fragment_loan_main_btn_refresh_status).setOnClickListener(v -> {
            isRefresh = true;
            HttpManager_pudamoney.getInstance().requestUserProcess();
        });

        view.findViewById(R.id.fragment_loan_main_layout_apply_btn).setOnClickListener(v -> {
            HttpManager_pudamoney.getInstance().saveUserBuriedPoint((this.getClass().getSimpleName() + "_CLICK").toUpperCase());
            if (PermissionUtil_pudamoney.checkLaunchPermissions(requireActivity(), AppShowInfo_pudamoney.getInstance().getHome())) {
                //device info upload
                try {
                    if (UserInfoHelper_pudamoney.getInstance().didLogin()) {
                        AnalyticsUtil_pudamoney.INSTANCE.saveDeviceS2SInfo();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                //request usage stats permission
                if (AppShowInfo_pudamoney.getInstance().needRequestUsageStatsPermission()) {
                    AnalyticsUtil_pudamoney.INSTANCE.checkUsageStatsPermission(BaseApplication_pudamoney.getApplication_pudamoney(), permissionGrant -> {
                        if (permissionGrant) {
                            //已经同意权限
                            //继续走业务流程
                            //all done, request user state
                            HttpManager_pudamoney.getInstance().requestUserProcess();

                        } else {
                            //不同意权限
                            //1 展示UI部分--》引导1--》引导2
                            //....
                            //....
                            Activity activity = requireActivity();
                            if (requireActivity() instanceof BaseActivity_pudamoney)
                                ((BaseActivity_pudamoney)activity).startActivity_pudamoney(UsagePermissionGuideActivity_pudamoney.class);
                        }
                        return null;
                    });
                } else {
                    //all done, request user state
                    HttpManager_pudamoney.getInstance().requestUserProcess();
                }
            } else {
                EasyPermissions.requestPermissions(this, getString(R.string.launch_permissions), PermissionUtil_pudamoney.REQ_CODE_PERMISSIONS, AppShowInfo_pudamoney.getInstance().getHome());
            }
        });

        view.findViewById(R.id.fragment_loan_main_ib_customer_service).setOnClickListener(v -> new CustomerServiceDialog_pudamoney(requireContext()).show());

        HttpManager_pudamoney.getInstance().queryLoanLimit();

    }

    @SuppressLint("SetTextI18n")
    public void updateLoanAmount_pudamoney() {
        isRefresh = false;
        loanAmountTv.setText(StringUtil_pudamoney.getSafeString(Constant_pudamoney.Money_Unit_Nigeria).toUpperCase() + " "
                + LoanLimitInfo_pudamoney.getInstance().getLimitAmount());
    }

}